﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Backend.JYD.EFDAL.DAL
{
    public class AutoBidSettingDAL : IDAL.Bid.IAutoBid
    {
        public long AddAutoBidSetting(Model.Bid.AutoBidSetting setting)
        {
            using (JydDataContext db = new JydDataContext())
            {
                setting.CreateTime = DateTime.Now;
                db.AutoBidSettings.Add(setting);
                db.SaveChanges();
                return setting.AutoBidId;
            }
        }

        public bool UpdateAutoBidSetting(Model.Bid.AutoBidSetting setting)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.AutoBidSettings.Where(p => p.AutoBidId == setting.AutoBidId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    temp.UserId = setting.UserId;
                    temp.IsValid = setting.IsValid;
                    temp.AutoType = setting.AutoType;
                    temp.BidMoney = setting.BidMoney;
                    temp.BidRate = setting.BidRate;
                    temp.TopLimit = setting.TopLimit;
                    temp.TermConfine = setting.TermConfine;
                    temp.RateConfine = setting.RateConfine;
                    temp.UpdateTime = DateTime.Now;
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public Model.Bid.AutoBidSetting GetAutoBidSettingById(long autoBidId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.AutoBidSettings.Where(p => p.AutoBidId == autoBidId);
                return query.FirstOrDefault();
            }
        }

        public IList<Model.Bid.AutoBidSetting> GetAutoBidSettingByUserId(long userId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.AutoBidSettings.Where(p => p.UserId == userId);
                return query.ToList();
            }
        }

        public IList<Model.Bid.AutoBidSetting> GetValidAutoBidSetting()
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.AutoBidSettings.Where(p => p.IsValid == true);
                return query.ToList();
            }
        }   
    }
}
